package com.tools.core;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import org.apache.commons.lang3.StringUtils;

public class ParseEntitySet {
	protected static boolean isInsertId = false; // 是否填充了ID的HTML
	private InitHtml initHtml = new InitHtml();

	/**
	 * 解析方法上的注解（实际上，要使得该注解解析更通用，需要在该类上封装一个通用调用，方法上没有，则找字段）
	 * 
	 * @param type
	 * @param clazz
	 * @return
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 * @throws IllegalArgumentException
	 * @throws InvocationTargetException
	 * @throws NoSuchMethodException
	 * @throws SecurityException
	 */
	public String parseMethod(String type, Class clazz)
			throws InstantiationException, IllegalAccessException,
			IllegalArgumentException, InvocationTargetException,
			NoSuchMethodException, SecurityException {
		Object obj = clazz.getConstructor(new Class[] {}).newInstance(
				new Object[] {});
		StringBuffer trs = new StringBuffer();
		for (Method method : clazz.getDeclaredMethods()) {
			EntitySet entitySet = method.getAnnotation(EntitySet.class);
			if (null != entitySet) {
				if ("edit".equals(type.toLowerCase())) {
					trs.append("\n");
					trs.append(initHtml.initEdit(clazz, method.getName(),
							entitySet));
				} else if ("list".equals(type.toLowerCase())) {
					trs.append("\n");
					trs.append(initHtml.initList(method, null, entitySet));
				} else if ("query".equals(type.toLowerCase())
						&& !StringUtils.isEmpty(entitySet.queryBy())) {
					trs.append(initHtml.initQuery(clazz, method, null,
							entitySet));
				} else if ("queryjs".equals(type.toLowerCase())
						&& !StringUtils.isEmpty(entitySet.queryBy())) {
					// 临时保存
					String temptrs = trs.toString();
					if (!StringUtils.isEmpty(temptrs.trim())) {
						trs.append(",");
					}
					// 存值，该值在后面会改变，故会转存另一对象，用于保存实际值
					String queryOne = initHtml.initQueryJs(clazz, method, null,
							entitySet).toString();
					// 转存
					String queryReal = queryOne;
					if (!StringUtils.isEmpty(queryOne.trim())) {
						trs.append(queryReal);
					}
				} else if ("detail".equals(type.toLowerCase())) {
					trs.append("\n");
					trs.append(initHtml.initDetail(clazz, method.getName(),
							entitySet));
				}
			}
		}
		return trs.toString();
	}

	/**
	 * 解析字段上的注解,返回HTML
	 * 
	 * @param type
	 * @param clazz
	 * @return
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 * @throws IllegalArgumentException
	 * @throws InvocationTargetException
	 * @throws NoSuchMethodException
	 * @throws SecurityException
	 */
	public String parseField(String type, Class clazz)
			throws InstantiationException, IllegalAccessException,
			IllegalArgumentException, InvocationTargetException,
			NoSuchMethodException, SecurityException {
		Object obj = clazz.getConstructor(new Class[] {}).newInstance(
				new Object[] {});
		StringBuffer trs = new StringBuffer();
		for (Field field : clazz.getDeclaredFields()) {
			EntitySet entitySet = field.getAnnotation(EntitySet.class);
			if (null != entitySet) {

				if ("edit".equals(type.toLowerCase())) {
					trs.append("\n");
					trs.append(initHtml.initEdit(clazz, field.getName(),
							entitySet));
				} else if ("list".equals(type.toLowerCase())) {
					trs.append("\n");
					trs.append(initHtml.initList(null, field, entitySet));
				} else if ("query".equals(type.toLowerCase())
						&& !StringUtils.isEmpty(entitySet.queryBy())) {
					trs.append(initHtml
							.initQuery(clazz, null, field, entitySet));
				} else if ("queryjs".equals(type.toLowerCase())
						&& !StringUtils.isEmpty(entitySet.queryBy())) {
					if (!StringUtils.isEmpty(trs.toString())) {
						trs.append(",");
					}
					trs.append(initHtml.initQueryJs(clazz, null, field,
							entitySet));
				} else if ("detail".equals(type.toLowerCase())) {
					trs.append("\n");
					trs.append(initHtml.initDetail(clazz, field.getName(),
							entitySet));
				}
			}
		}
		return trs.toString();
	}

	public static void main(String[] args) throws InstantiationException,
			IllegalAccessException, IllegalArgumentException,
			InvocationTargetException, NoSuchMethodException, SecurityException {
		ParseEntitySet ps = new ParseEntitySet();
		// System.out.println(ps.parseField("edit", Employee.class));
	}
}
